package com.hotel.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hotel.dao.mapper.OrderMapper;
import com.hotel.entity.Order;
import com.hotel.entity.OrderExample;
import com.hotel.entity.SelectMessage;
import com.hotel.service.OrderInfoService;
import com.hotel.utils.Message;



@Service("orderInfoService")
public class OrderInfoServiceImpl implements OrderInfoService {
	
	@Resource
	private OrderMapper orderMapper;

	@Override
	public List<Order> SelectOrderInfo(SelectMessage selectMessage) {
		
		OrderExample example = new OrderExample();
		OrderExample.Criteria criteria = example.createCriteria();
		
		if(selectMessage.getType() == "user_id"){
			int keyword = Integer.parseInt(selectMessage.getKeyword());
			
			if(selectMessage.getOrderType() == "star"){
				if(selectMessage.getSort() == "DESC"){
					criteria.andUserIdEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());
					example.setOrderByClause("star");
					example.setSort("DESC");
				}
				else if(selectMessage.getSort() =="ASC"){
					criteria.andUserIdEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());
					example.setOrderByClause("star");
					example.setSort("ASC");
				}
			}
			else if(selectMessage.getOrderType() == "reserve_time"){
				if(selectMessage.getSort() == "DESC"){
					criteria.andUserIdEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("reserve_time");
					example.setSort("DESC");
				}
				else if(selectMessage.getSort() =="ASC"){
					criteria.andUserIdEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("reserve_time");
					example.setSort("ASC");
				}
			}
			else if(selectMessage.getOrderType() == "room_id"){
				if(selectMessage.getSort() == "DESC"){
					criteria.andUserIdEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("room_id");
					example.setSort("DESC");
				}
				else if(selectMessage.getSort() =="ASC"){
					criteria.andUserIdEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("room_id");
					example.setSort("ASC");
				}
			}
		}
		else if(selectMessage.getType() == "order_no"){
			String keyword = selectMessage.getKeyword();
			
			if(selectMessage.getOrderType() == "star"){
				if(selectMessage.getSort() == "DESC"){
					criteria.andOrderNoEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("star");
					example.setSort("DESC");
				}
				else if(selectMessage.getSort() =="ASC"){
					criteria.andOrderNoEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("star");
					example.setSort("ASC");
				}
			}
			else if(selectMessage.getOrderType() == "reserve_time"){
				if(selectMessage.getSort() == "DESC"){
					criteria.andOrderNoEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("reserve_time");
					example.setSort("DESC");
				}
				else if(selectMessage.getSort() =="ASC"){
					criteria.andOrderNoEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("reserve_time");
					example.setSort("ASC");
				}
			}
			else if(selectMessage.getOrderType() == "room_id"){
				if(selectMessage.getSort() == "DESC"){
					criteria.andOrderNoEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("room_id");
					example.setSort("DESC");
				}
				else if(selectMessage.getSort() =="ASC"){
					criteria.andOrderNoEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("room_id");
					example.setSort("ASC");
				}
			}
		}
		else if(selectMessage.getType() == "room_id"){
			int keyword = Integer.parseInt(selectMessage.getKeyword());
		
			if(selectMessage.getOrderType() == "star"){
				if(selectMessage.getSort() == "DESC"){
					criteria.andRoomIdEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("star");
					example.setSort("DESC");
				}
				else if(selectMessage.getSort() =="ASC"){
					criteria.andRoomIdEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("star");
					example.setSort("ASC");
				}
			}
			else if(selectMessage.getOrderType() == "reserve_time"){
				if(selectMessage.getSort() == "DESC"){
					criteria.andRoomIdEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("reserve_time");
					example.setSort("DESC");
				}
				else if(selectMessage.getSort() =="ASC"){
					criteria.andRoomIdEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("reserve_time");
					example.setSort("ASC");
				}
			}
			else if(selectMessage.getOrderType() == "room_id"){
				if(selectMessage.getSort() == "DESC"){
					criteria.andRoomIdEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("room_id");
					example.setSort("DESC");
				}
				else if(selectMessage.getSort() =="ASC"){
					criteria.andRoomIdEqualTo(keyword);
					criteria.andReserveTimeBetween(selectMessage.getStartTime(), selectMessage.getEndTime());

					example.setOrderByClause("room_id");
					example.setSort("ASC");
				}
			}
		}
		
		

		List<Order> i = orderMapper.selectByExample(example);
		return i;
	}

	@Override
	public String updateOrderInfo(Order order) {
		
		String message;
		OrderExample example = new OrderExample();
		OrderExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(order.getId());
		
		if(1 == orderMapper.updateByExample(order, example)){
			message = Message.Message_7002;
		}
		else{
			message = Message.Message_8004;
		}
		return message;
	}

	@Override
	public String evaluateOrderInfo(Order order) {
		String message;
		OrderExample example = new OrderExample();
		OrderExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(order.getId());
		
		if(1 == orderMapper.updateByExample(order, example)){
			message = Message.Message_7003;
		}
		else{
			message = Message.Message_8005;
		}
		return message;
	}

}
